package com.nest.WorkNest.tools;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * 类型转化工具
 */
public class ParseUtils {

    /**
     * String转换成int
     *
     * @param num
     * @return
     */
    public static int string2int(String num) {

        try {
            return Integer.parseInt(num);
        } catch (Exception e) {
            // TODO: handle exception
            return 0;
        }
    }

    /**
     * String转换成long
     *
     * @param num
     * @return
     */
    public static long string2long(String num) {

        try {
            return Long.parseLong(num);
        } catch (Exception e) {
            // TODO: handle exception
            return 0;
        }
    }

    /**
     * String转换成float
     *
     * @param num
     * @return
     */
    public static float string2float(String num) {

        try {
            return Float.parseFloat(num);
        } catch (Exception e) {
            // TODO: handle exception
            return 0.00f;
        }

    }

    /**
     * String转换成floats
     *
     * @param num
     * @return
     */
    public static String string2floats(String num) {

        try {
            return new BigDecimal(num) + "";
        } catch (Exception e) {
            // TODO: handle exception
            return "0.00";
        }

    }

    /**
     * String转换成floats
     *
     * @param num
     * @return
     */
    public static String string2wanfloats(String num) {

        try {
            return string2float(num) / 10000 + "";
        } catch (Exception e) {
            // TODO: handle exception
            return "0.00";
        }

    }

    /**
     * String转换成floatString
     *
     * @param num
     * @return
     */
    public static String string2floatString(String num) {

        try {
            return Float.parseFloat(num) + "";
        } catch (Exception e) {
            // TODO: handle exception
            return "0.00";
        }

    }

    /**
     * String转换成double
     *
     * @param num
     * @return
     */
    public static double string2double(String num) {

        try {
            return Double.parseDouble(num);
        } catch (Exception e) {
            // TODO: handle exception
            return 0.00d;
        }

    }

    /**
     * 两个String相减（float�?
     *
     * @param num1
     * @param num2
     * @return
     */
    public static String subtractFloatString(String num1, String num2) {

        String n = (new BigDecimal(num1).subtract(new BigDecimal(num2))) + "";

        return n;

    }

    /**
     * 两个String相乘（float�?
     *
     * @param num1
     * @param num2
     * @return
     */
    public static String multiplyFloatString(String[] num) {
        String n = "0";

        for (int i = 0; i < num.length; i++) {
            if (i == 0) {

                n = (new BigDecimal(num[0])) + "";
            } else {
                if (n.equals("0")) {
                    n = "1";
                }
                n = (new BigDecimal(n).multiply(new BigDecimal(num[i]))) + "";
            }
        }

        return n;

    }

    /**
     * 两个String相除（float）（num/num2�?
     *
     * @param num1
     * @param num2
     * @return
     */
    public static String divideFloatString(String num1, String num2) {

        String n = (new BigDecimal(num1).divide(new BigDecimal(num2), 2,
                BigDecimal.ROUND_HALF_UP)) + "";

        return n;

    }

    /**
     * 两个String相加 (float)
     *
     * @param num1
     * @param num2
     * @return
     */
    public static String plusFloatString(String num1, String num2) {

        String n = (new BigDecimal(num1).add(new BigDecimal(num2))) + "";

        return n;

    }

    /**
     * 两个String转成int对比 （num1>num2�?
     *
     * @param num1
     * @param num2
     * @return
     */
    public static boolean compareInt(String num1, String num2) {

        if (string2int(num1) >= string2int(num2)) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 两个String转成int相加 （num1>num2�?
     *
     * @param num1
     * @param num2
     * @return
     */
    public static int plusInt(String num1, String num2) {

        return string2int(num1) + string2int(num2);

    }

    /**
     * 两个String转成float对比 （num1>num2�?
     *
     * @param num1
     * @param num2
     * @return
     */
    public static boolean compareFloatSingle(String num1, String num2) {
        if (string2float(num1) > string2float(num2)) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 两个String转成float对比 （num1>=num2�?
     *
     * @param num1
     * @param num2
     * @return
     */
    public static boolean compareFloat(String num1, String num2) {

        if (string2float(num1) >= string2float(num2)) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 两个String转成float相加 （num1>num2�?
     *
     * @param num1
     * @param num2
     * @return
     */
    public static float plusFloat(String num1, String num2) {

        return string2float(num1) + string2float(num2);

    }

    /**
     * 两个String转成int对比 （num1>num2�?
     *
     * @param num1
     * @param num2
     * @return
     */
    public static boolean compareDouble(String num1, String num2) {

        if (string2double(num1) >= string2double(num2)) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 两个String转成double相加 （num1>num2�?
     *
     * @param num1
     * @param num2
     * @return
     */
    public static double plusDouble(String num1, String num2) {

        return string2double(num1) + string2double(num2);

    }

    /**
     * double转化String 如果小数后面不为�? 取两位有效数�? 如果为零 取整�?
     *
     * @param num1
     * @param num2
     * @return
     */
    public String plusDouble(double num) {

        if (num * 100 % 100 > 0) {
            DecimalFormat df = new DecimalFormat("#.00");
            return df.format(num);
        } else {
            DecimalFormat df = new DecimalFormat("#");
            return df.format(num);
        }
    }

}
